<?php

/* ----------------------------------------------
  Caloris: Administration Users Editor

  $Id$
  $Author$

  Users: Detailed settings
  ---------------------------------------------- */

require_once("../../../index.php");
Caloris\ACL::authenticate();

define('_MODULE_ACTIVE', 'usermanager');

if ($_REQUEST["function_name"] == 'users_edit') {
    if (Caloris\ACL::authorize("users") == 1) {
        // Checks the validity of e-mail
        $userName = $_POST["username"];
        $attributes = $_POST["attributes"];
        $email = $_POST["email"];

        if (\Nette\Utils\Validators::isEmail($email) == false) {
            header("location: " . _CALSET_PATHS_URI . _CALSET_DIRS_MODULE . "/usermanager/users.detail.php?idf=$userName&msg=" . urlencode("Nesprávný formát e-mailu"));
            exit();
        }

        $xmlUser = simplexml_load_file(_CALSET_PATHS_BASE . _CALSET_DIRS_DATA . '/users.xml');
        $xmlUserEdit = $xmlUser->xpath('//user[@id="' . $userName . '"]');

        if (count($xmlUserEdit) == 0) {
            header("location: " . _CALSET_PATHS_URI . _CALSET_DIRS_MODULE . "/" . _MODULE_ACTIVE . "/users.detail.php?idf=$userName&msg=" . urlencode("Problém s uživatelským jménem"));
            exit();
        }

        $xmlUserEdit[0]->email = $email;
        $xmlUserEdit[0]->state->permissions = \Caloris\Tools::checknull($attributes);

        $xmlUser->asXML(_CALSET_PATHS_BASE . _CALSET_DIRS_DATA . '/users.xml');

        header("location: " . $_SERVER["HTTP_REFERER"] . "");
        exit();
    } else {
        header("location: " . _CALSET_PATHS_URI . _CALSET_DIRS_MODULE . "/" . _MODULE_ACTIVE . "/users.detail.php?idf=" . $_POST["id"] . "&msg=" . urlencode("Nemáte oprávnění ke změnám v uživatelské oblasti"));
        exit();
    }
} elseif ($_REQUEST["function_name"] == 'passchange') {

    $acl = new Caloris\ACL();
    $acl->passChange('', $_POST["pwd"], $_POST["pwd2"], 'admin');

    header("location: " . _CALSET_PATHS_URI . _CALSET_DIRS_MODULE . "/" . _MODULE_ACTIVE . "/users.detail.php?idf=" . $_POST["idf"] . "&msg=" . urlencode($acl->message()) . "");
    exit();
}

$idf = $_REQUEST["idf"];

$xmlUser = simplexml_load_file(_CALSET_PATHS_BASE . _CALSET_DIRS_DATA . '/users.xml');
$xma = $xmlUser->xpath('//user[@id="' . $idf . '"]');

$content = '
<h1>Uživatel</h1>

<form action="' . _CALSET_PATHS_URI . _CALSET_DIRS_MODULE . '/usermanager/users.detail.php" method="post" class="form-horizontal">
<input type="hidden" name="function_name" value="users_edit"  />
<input type="hidden" name="id" value="' . $xma[0]["id"] . '"  />
    
<div class="control-group">
<label class="control-label">Uživatelské jméno</label>
<div class="controls"><input type="text" name="username" value="' . $xma[0]["id"] . '" class="input-xxlarge" /></div>
</div>

<div class="control-group">
<label class="control-label">Oprávnění</label>
<div class="controls">
';

if ($xma[0]["admin"] == 1) {
    $xmlPermissions = simplexml_load_file(_CALSET_PATHS_BASE . _CALSET_DIRS_DATA . '/permissions.xml');
    $xpc = (array) $xmlPermissions;
    $xpc = $xpc["permission"];
    natcasesort($xpc);
    $xpc = array_values($xpc);

    if (count($xpc) > 0) {
        $content .= '<select name="attributes">';
        for ($a = 0; $a < count($xpc); $a++) {
            if (strcmp($xma[0]->state->permissions, $xpc[$a]["id"]) == 0) {
                $content .= '
<option value="' . $xpc[$a]["id"] . '" selected="selected">' . $xpc[$a]->name . '</option>';
            } else {
                $content .= '
<option value="' . $xpc[$a]["id"] . '">' . $xpc[$a]->name . '</option>';
            }
        }
        $content .= '
</select>';
    }
} else {
    $content .= '<input type="hidden" name="attributes" value="3" /> Člen';
}

$content .= '
</div>
</div>

<div class="control-group">
<label class="control-label">E-mail</label>
<div class="controls"><input type="text" name="email" value="' . $xma[0]->email . '" class="input-xxlarge" /></div>
</div>

<div class="control-group">
<div class="controls"><input type="submit"  value="Uložit změny" class="btn btn-primary" /></div>
</div>
</form>';

if (strcmp($xma[0]["id"], $_COOKIE["auser"]) == 0) {
    $content .= '
<h2>Změna hesla</h2>

<form action="' . _CALSET_PATHS_URI . _CALSET_DIRS_MODULE . '/usermanager/users.detail.php" method="post" class="form-horizontal">
<input type="hidden" name="function_name" value="passchange"  />
<input type="hidden" name="idf" value="' . $idf . '"  />
<input type="hidden" name="username" value="' . $xma[0]["id"] . '"  />
<input type="hidden" name="id" value="' . $xma[0]["id"] . '"  />
    
<div class="control-group">
<label class="control-label">Heslo</label>
<div class="controls"><input type="password" name="pwd" /></div>
</div>

<div class="control-group">
<label class="control-label">Zopakujte znovu heslo</label>
<div class="controls"><input type="password" name="pwd2" /></div>
</div>

<div class="control-group">
<div class="controls"><input type="submit"  value="Změnit heslo" class="btn btn-primary" /></div>
</div>
</form>' . PHP_EOL;
}

$content .= '
<h2>Kontakt</h2>';

$xmlContact = simplexml_load_file(_CALSET_PATHS_BASE . _CALSET_DIRS_DATA . '/contacts.xml');
$xmlContactList = $xmlContact->xpath('//contact[@connect="' . $xma[0]["id"] . '"]');

if (count($xmlContactList) > 0) {
    if ($xmlContactList[0]->show == 1) {
        $show = 'Ano';
    } else {
        $show = 'Ne';
    }

    $content .= '
<p><a href="' . _CALSET_PATHS_URI . _CALSET_DIRS_MODULE . '/contactmanager/contacts.detail.php?connect=' . $xma[0]["id"] . '">Editovat přiřazený kontakt</a></p>

<table class="table table-striped">
    <tr>
        <td style="width: 250px;">Jméno/Název společnosti</td>
        <td>' . $xmlContactList[0]->name . '</td>
    </tr>
    <tr>
        <td>Funkce</td>
        <td>' . $xmlContactList[0]->grade . '</td>
    </tr>
    <tr>
        <td>E-mail</td>
        <td>';

    for ($b = 0; $b < count($xmlContactList[0]->emails->email); $b++) {
        $content .= '<p class="newtext">' . $xmlContactList[0]->emails->email[$b]["type"] . ': ' . $xmlContactList[0]->emails->email[$b] . '</p>';
    }

    $content .= '</td>
    </tr>
    <tr>
        <td>Adresa</td>
        <td>' . $xmlContactList[0]->address . '<br />
' . $xmlContactList[0]->zip . ' ' . $xmlContactList[0]->city . '</td>
    </tr>
    <tr>
        <td>Kraj</td>
        <td>' . $xmlContactList[0]->district . '</td>
    </tr>
    <tr>
        <td>Země</td>
        <td>' . $xmlContactList[0]->country . '</td>
    </tr>
    <tr>
        <td>Telefon</td>
        <td>';

    for ($b = 0; $b < count($xmlContactList[0]->phone->number); $b++) {
        $content .= '<p class="newtext">' . $xmlContactList[0]->phone->number[$b]["type"] . ': ' . $xmlContactList[0]->phone->number[$b] . '</p>';
    }

    $content .= '</td>
    </tr>
    <tr>
        <td>IČ</td>
        <td>' . $xmlContactList[0]->ic . '</td>
    </tr>
    <tr>
        <td>DIČ</td>
        <td>' . $xmlContactList[0]->dic . '</td>
    </tr>
    <tr>
        <td>Zobrazit v kontaktech</td>
        <td>' . $show . '</td>
    </tr>
</table>
';
} else {
    $content .= '<form action="' . _CALSET_PATHS_URI . _CALSET_DIRS_MODULE . '/contactmanager/index.php" id="rpp" method="post" class="well">
<input type="hidden" name="function_name" value="contactInsert" />
<input type="hidden" name="idfu" value="' . $xma[0]["id"] . '" />
<input type="submit"  value="Přidat kontakt" class="btn btn-primary" />
</form>' . PHP_EOL;
}

$data["title"] = 'Uživatelé: Detail';

$tmpl = new Caloris\Template;
$tmpl->input($content);
$tmpl->renderHead($data);
echo $tmpl->renderBody('caloris-admin');